package com.xiaozhameng.bootstudy.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

import java.util.concurrent.TimeUnit;

import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * mysql配置 - 不起用
 */
@Configuration
@MapperScan(basePackages = "com.xiaozhameng.bootstudy.dao")
public class DataSourceConfigurationTemp {

    /**
     * 数据库连接池
     */
    @Bean(name = "dataSource")
    public HikariDataSource dataSource() {
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
        hikariConfig.setUsername("root");
        hikariConfig.setPassword("root");

        hikariConfig.setPoolName("user_pool");
        // 最大连接数
        hikariConfig.setMaximumPoolSize(10);
        hikariConfig.setMinimumIdle(2);
        hikariConfig.setConnectionTimeout(20 * 1000);
        hikariConfig.setIdleTimeout(1 * 60  * 60 * 1000);
        hikariConfig.setJdbcUrl("jdbc:mysql://mysql.local:3306/xiaozhameng?" +
                "useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&serverTimezone=UTC");
        return new HikariDataSource(hikariConfig);
    }

    /**
     * 事物管理器
     */
    @Bean(name = "transactionManager")
    @Primary
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(this.dataSource());
    }
}
